﻿using Furion.DatabaseAccessor;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using Furion.UnifyResult;
using iWare.Wms.Core;
using Mapster;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;

namespace iWare.Wms.Application
{
    /// <summary>
    /// 入库管理
    /// </summary>
    [ApiDescriptionSettings("Pda仓库作业", Name = "PdaBindEntrance", Order = 100)]
    [Route("api/PdaBindEntrance")]
    public class PdaBindEntranceService : IDynamicApiController, ITransient
    {
        private readonly IRepository<WmsContainer, MasterDbContextLocator> _wmsContainerRep;
        private readonly IRepository<WmsMaterial, MasterDbContextLocator> _wmsMaterialRep;
        private readonly IRepository<WmsMaterialContainer, MasterDbContextLocator> _wmsMaterialContainerRep;
        private readonly IRepository<WmsArea, MasterDbContextLocator> _wmsAreaRep;
        private readonly IRepository<WmsPlace, MasterDbContextLocator> _wmsPlaceRep;
        private readonly IRepository<WmsTask, MasterDbContextLocator> _wmsTaskRep;
        private readonly IRepository<WmsMaterialStock, MasterDbContextLocator> _wmsMaterialStockRep;
        private readonly IRepository<WmsContainerPlace, MasterDbContextLocator> _wmsContainerPlaceRep;

        public PdaBindEntranceService(
            IRepository<WmsContainer, MasterDbContextLocator> wmsContainerRep,
            IRepository<WmsMaterial, MasterDbContextLocator> wmsMaterialRep,
            IRepository<WmsMaterialContainer, MasterDbContextLocator> wmsMaterialContainerRep,
            IRepository<WmsArea, MasterDbContextLocator> wmsAreaRep,
            IRepository<WmsPlace, MasterDbContextLocator> wmsPlaceRep,
            IRepository<WmsTask, MasterDbContextLocator> wmsTaskRep,
            IRepository<WmsMaterialStock, MasterDbContextLocator> wmsMaterialStockRep,
            IRepository<WmsContainerPlace, MasterDbContextLocator> wmsContainerPlaceRep
        )
        {
            _wmsContainerRep = wmsContainerRep;
            _wmsMaterialRep = wmsMaterialRep;
            _wmsMaterialContainerRep = wmsMaterialContainerRep;
            _wmsAreaRep = wmsAreaRep;
            _wmsPlaceRep = wmsPlaceRep;
            _wmsTaskRep = wmsTaskRep;
            _wmsMaterialStockRep = wmsMaterialStockRep;
            _wmsContainerPlaceRep = wmsContainerPlaceRep;
        }


        /// <summary>
        /// 获取托盘信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("GetContainer")]
        [UnifyResult(typeof(PdaBindEntranceOutput))]
        public async Task<object> GetContainer([FromQuery] PdaBindEntranceSearchContainer input)
        {
            var containerModel = await _wmsContainerRep.FirstOrDefaultAsync(n => n.ContainerCode == input.Containercode && n.ContainerStatus != Core.Enum.ContainerStatus.JINYONG);
            if (containerModel == null) return XnRestfulResultProvider.RESTfulMesaage("托盘信息不存在!");

            var isExit = await _wmsTaskRep.AnyAsync(p => p.ContainerCode == containerModel.ContainerCode && p.TaskStatus != Core.Enum.TaskStatus.WANCHENG);
            if (isExit) return XnRestfulResultProvider.RESTfulMesaage("托盘存在未完成任务!");


            var bindEntranceOutputModel = new PdaBindEntranceOutput();
            bindEntranceOutputModel.WmsContainer = containerModel.Adapt<WmsContainerDto>();
            bindEntranceOutputModel.Containercode = containerModel.ContainerCode;
            if (containerModel.ContainerStatus != Core.Enum.ContainerStatus.KOUXIAN)
            {
                bindEntranceOutputModel.WmsMaterials = await _wmsMaterialContainerRep
                    .Where(p => p.ContainerCode == containerModel.ContainerCode && p.BindStatus == CommonStatus.ENABLE)
                    .ProjectToType<PdaWmsMaterialContainerOutput>().ToListAsync();

            }
            return XnRestfulResultProvider.RESTfulResult(bindEntranceOutputModel);
        }

        /// <summary>
        /// 获取物料信息  
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("GetMaterial")]
        [UnifyResult(typeof(PdaWmsMaterialContainerOutput))]
        public async Task<object> GetMaterial([FromQuery] PdaBindEntranceSearchMateria input)
        {
            var materialModel = await _wmsMaterialRep.FirstOrDefaultAsync(n => n.MaterialNo == input.Materialno);
            if (materialModel == null) return XnRestfulResultProvider.RESTfulMesaage("物料信息不存在!");
            var model = materialModel.Adapt<PdaWmsMaterialContainerOutput>();
            model.Materialid = materialModel.Id;
            return XnRestfulResultProvider.RESTfulResult(model);
        }


        /// <summary>
        /// 执行组盘操作
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("BindEntrance")]
        [UnitOfWork]
        public async Task<PdaBindEntranceOkOutput> BindEntrance(PdaAddBindEntranceInput input)
        {
            //托盘信息
            var containerModel = await _wmsContainerRep.FirstOrDefaultAsync(n => n.ContainerCode == input.Containercode && n.ContainerStatus != Core.Enum.ContainerStatus.JINYONG);
            if (containerModel == null) throw Oops.Oh("托盘信息不存在!");

            if (containerModel.ContainerStatus == Core.Enum.ContainerStatus.KUWEI) throw Oops.Oh("托盘存在库位中!");

            var isExit = await _wmsTaskRep.AnyAsync(p => p.ContainerCode == containerModel.ContainerCode && p.TaskStatus != Core.Enum.TaskStatus.WANCHENG);
            if (isExit) throw Oops.Oh("托盘存在未完成任务中!");

            isExit = await _wmsContainerPlaceRep.AnyAsync(p => p.ContainerCode == containerModel.ContainerCode && p.ContainerPlaceStatus == CommonStatus.ENABLE);
            if (isExit) throw Oops.Oh("托盘存在库位中!");

            //组盘信息
            var WmsMaterialContainerList = await _wmsMaterialContainerRep
                .Where(p => p.ContainerCode == input.Containercode && p.BindStatus == CommonStatus.ENABLE).ToListAsync();

            if (WmsMaterialContainerList.Count > 0)
            {
                foreach (var item in WmsMaterialContainerList)
                {
                    await _wmsMaterialContainerRep.DeleteAsync(item);
                }
            }
            //修改托盘状态
            containerModel.ContainerStatus = Core.Enum.ContainerStatus.ZUPANG;
            await _wmsContainerRep.UpdateAsync(containerModel);
            //组盘单据
            var orderNo = Yitter.IdGenerator.YitIdHelper.NextId().ToString();
            foreach (var item in input.WmsMaterials)
            {
                var bindentranceModel = item.Adapt<WmsMaterialContainer>();
                bindentranceModel.BindStatus = CommonStatus.ENABLE;
                bindentranceModel.OrderNo = orderNo;
                bindentranceModel.ContainerCode = containerModel.ContainerCode;
                bindentranceModel.ContainerId = containerModel.Id;
                bindentranceModel.Id = Yitter.IdGenerator.YitIdHelper.NextId();
                await _wmsMaterialContainerRep.InsertNowAsync(bindentranceModel);
            }

            return new PdaBindEntranceOkOutput()
            {
                Containercode = input.Containercode,
            };
        }


        /// <summary>
        /// 获取入库口
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("GetEntrance")]
        public async Task<List<string>> GetEntrance()
        {
            return new List<string>()
            {
                "100","101","102"
            };
        }

        /// <summary>
        /// 手动入库
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("ManualWare")]
        [UnitOfWork]
        public async Task ManualWare(PdaAddManualWareInput input)
        {
            //检查托盘
            var containerModel = await _wmsContainerRep.FirstOrDefaultAsync(p => p.ContainerCode == input.Containercode && p.ContainerStatus != Core.Enum.ContainerStatus.JINYONG);
            if (containerModel == null) throw Oops.Oh("托盘不存在!");

            var isExit = await _wmsTaskRep.AnyAsync(p => p.ContainerCode == containerModel.ContainerCode && p.TaskStatus != Core.Enum.TaskStatus.WANCHENG);
            if (isExit) throw Oops.Oh("托盘存在未完成任务中!");

            //删除组盘信息
            var oldwmsMaterialContainerList = await _wmsMaterialContainerRep
                .Where(p => p.ContainerCode == input.Containercode && p.BindStatus == CommonStatus.ENABLE).ToListAsync();
            if (oldwmsMaterialContainerList.Count > 0)
            {
                foreach (var item in oldwmsMaterialContainerList)
                {
                    await _wmsMaterialContainerRep.DeleteAsync(item);
                }

            }
            //组盘单据 默认空托单据
            var orderNo = "N/A";
            if (input.WmsMaterials.Count > 0) orderNo = Yitter.IdGenerator.YitIdHelper.NextId().ToString();

            //新增组盘数据
            foreach (var item in input.WmsMaterials)
            {
                var bindentranceModel = item.Adapt<WmsMaterialContainer>();
                bindentranceModel.BindStatus = CommonStatus.ENABLE;
                bindentranceModel.OrderNo = orderNo;
                bindentranceModel.ContainerCode = containerModel.ContainerCode;
                bindentranceModel.ContainerId = containerModel.Id;
                bindentranceModel.Id = Yitter.IdGenerator.YitIdHelper.NextId();
                await _wmsMaterialContainerRep.InsertNowAsync(bindentranceModel);
            }
            //更改托盘状态
            containerModel.ContainerStatus = Core.Enum.ContainerStatus.KUWEI;
            await _wmsContainerRep.UpdateAsync(containerModel);

            //检查库位并更新
            var placeModel = await _wmsPlaceRep.DetachedEntities.Where(p => p.PlaceCode == input.Placecode).ProjectToType<WmsPlace>().FirstOrDefaultAsync();
            if (placeModel == null) throw Oops.Oh("库位不存在!");
            if (placeModel.Islock == YesOrNot.Y) throw Oops.Oh("库位被锁定!");
            if (placeModel.PlaceStatus != Core.Enum.PlaceStatus.KONGXIAN) throw Oops.Oh("库位已存货!");

            placeModel.PlaceStatus = Core.Enum.PlaceStatus.CUNHUO;
            if (orderNo == "N/A") placeModel.EmptyContainer = YesOrNot.Y;
            else placeModel.EmptyContainer = YesOrNot.N;
            await _wmsPlaceRep.UpdateAsync(placeModel);

            //创建任务
            var takmodel = new WmsTask()
            {
                TaskNo = Yitter.IdGenerator.YitIdHelper.NextId().ToString(),
                TaskModel = Core.Enum.TaskModel.SHOUDONG,
                TaskType = Core.Enum.TaskType.RUKU,
                TaskLevel = 1,
                Aisle = placeModel.Aisle,
                TaskStatus = Core.Enum.TaskStatus.WANCHENG,
                ContainerCode = input.Containercode,
                SourcePlace = string.IsNullOrEmpty(input.Entrance) ? "N/A" : input.Entrance,
                ToPlace = placeModel.PlaceCode,
                AreaName = placeModel.WmsArea.AreaName,
                OrderNo = orderNo,
            };
            await _wmsTaskRep.InsertAsync(takmodel);

            //创建托盘库位关系表
            var wmsContainerPlaceModel = new WmsContainerPlace()
            {
                PlaceId = placeModel.Id,
                PlaceCode = placeModel.PlaceCode,
                ContainerId = containerModel.Id,
                ContainerCode = containerModel.ContainerCode,
                ContainerPlaceStatus = CommonStatus.ENABLE
            };
            await _wmsContainerPlaceRep.InsertAsync(wmsContainerPlaceModel);




            //更新库存
            var wmsMaterialContainerList = await _wmsMaterialContainerRep.DetachedEntities
                .Where(p => p.OrderNo == orderNo && p.BindStatus == CommonStatus.ENABLE).ProjectToType<WmsMaterialContainer>().ToListAsync();
            //物料入库
            foreach (var item in wmsMaterialContainerList)
            {
                var StockModel = await _wmsMaterialStockRep
                    .Where(p => p.ContainerCode == input.Containercode
                    && p.MaterialName == item.MaterialName && p.MaterialNo == item.MaterialNo && p.MaterialBatch == item.MaterialBatch).FirstOrDefaultAsync();
                if (StockModel != null)
                {
                    StockModel.PlaceCode = input.Placecode;
                    StockModel.AreaId = placeModel.WmsArea.Id;
                    StockModel.StockNumber = item.BindQuantity;
                    await _wmsMaterialStockRep.UpdateAsync(StockModel);
                }
                else
                {
                    StockModel = new WmsMaterialStock()
                    {
                        MaterialNo = item.WmsMaterial.MaterialNo,
                        MaterialType = item.WmsMaterial.MaterialType,
                        MaterialName = item.WmsMaterial.MaterialName,
                        MaterialSpec = item.WmsMaterial.MaterialSpec,
                        MaterialBatch = item.WmsMaterial.MaterialBatch,
                        InspectionMethod = item.WmsMaterial.InspectionMethod,
                        UnitType = item.WmsMaterial.UnitType,
                        UnitNo = item.WmsMaterial.UnitNo,
                        StockNumber = item.BindQuantity,
                        PlaceCode = input.Placecode,
                        ContainerCode = input.Containercode,
                        AreaId = placeModel.AreaId,
                    };
                    await _wmsMaterialStockRep.InsertAsync(StockModel);
                }
            }
            //空托入库
            if (orderNo == "N/A")
            {
                var StockModel = new WmsMaterialStock()
                {
                    MaterialNo = "N/A",
                    MaterialType = Core.Enum.MaterialType.KONGTUO,
                    MaterialName = "N/A",
                    MaterialSpec = "N/A",
                    MaterialBatch = "N/A",
                    InspectionMethod = Core.Enum.MaterialInspection.MIANJIAN,
                    UnitType = Core.Enum.UnitType.ZHONGLIANG,
                    UnitNo = Core.Enum.UnitnoType.T,
                    StockNumber = 1,
                    PlaceCode = input.Placecode,
                    ContainerCode = input.Containercode,
                    AreaId = placeModel.AreaId,
                };
                await _wmsMaterialStockRep.InsertAsync(StockModel);
            }
        }


        /// <summary>
        /// 自动入库
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("AutoWare")]
        [UnitOfWork]
        public async Task AutoWare(PdaAddAutoWareInput input)
        {
            //检查托盘
            var containerModel = await _wmsContainerRep.FirstOrDefaultAsync(p => p.ContainerCode == input.Containercode && p.ContainerStatus != Core.Enum.ContainerStatus.JINYONG);
            if (containerModel == null) Oops.Oh("托盘不存在!");

            var isExit = await _wmsTaskRep.AnyAsync(p => p.ContainerCode == containerModel.ContainerCode && p.TaskStatus != Core.Enum.TaskStatus.WANCHENG);
            if (isExit) Oops.Oh("托盘存在未完成任务中!");

            //删除组盘信息
            var oldwmsMaterialContainerList = await _wmsMaterialContainerRep
                .Where(p => p.ContainerCode == input.Containercode && p.BindStatus == CommonStatus.ENABLE).ToListAsync();

            if (oldwmsMaterialContainerList.Count > 0)
            {
                foreach (var item in oldwmsMaterialContainerList)
                {
                    await _wmsMaterialContainerRep.DeleteAsync(item);
                }
            }

            //组盘单据号
            var orderNo = "N/A";
            if (input.WmsMaterials.Count > 0) orderNo = Yitter.IdGenerator.YitIdHelper.NextId().ToString();
            //创建新组盘记录
            foreach (var item in input.WmsMaterials)
            {
                var bindentranceModel = item.Adapt<WmsMaterialContainer>();
                bindentranceModel.BindStatus = CommonStatus.ENABLE;
                bindentranceModel.OrderNo = orderNo;
                bindentranceModel.ContainerCode = containerModel.ContainerCode;
                bindentranceModel.ContainerId = containerModel.Id;
                bindentranceModel.Id = Yitter.IdGenerator.YitIdHelper.NextId();
                await _wmsMaterialContainerRep.InsertNowAsync(bindentranceModel);
            }
            //修改托盘状态
            if (input.WmsMaterials.Count > 0) containerModel.ContainerStatus = Core.Enum.ContainerStatus.ZUPANG;
            else containerModel.ContainerStatus = Core.Enum.ContainerStatus.KOUXIAN;
            await _wmsContainerRep.UpdateAsync(containerModel);

            //库位
            var placeModel = new WmsPlace();
            if (string.IsNullOrEmpty(input.Placecode))
            {
                placeModel = await _wmsPlaceRep.DetachedEntities.Where(p => p.PlaceStatus == Core.Enum.PlaceStatus.KONGXIAN && p.Islock == YesOrNot.N)
                    .ProjectToType<WmsPlace>().FirstOrDefaultAsync();
                if (placeModel == null) Oops.Oh("库位不存在!");
                input.Placecode = placeModel.PlaceCode;
            }
            else
            {
                //库位
                placeModel = await _wmsPlaceRep.DetachedEntities.Where(p => p.PlaceCode == input.Placecode).ProjectToType<WmsPlace>().FirstOrDefaultAsync();
                if (placeModel == null) Oops.Oh("库位不存在!");
                if (placeModel.Islock == YesOrNot.Y) Oops.Oh("库位被锁定!");
                if (placeModel.PlaceStatus != Core.Enum.PlaceStatus.KONGXIAN) Oops.Oh("库位已存货!");
            }
            //更新库位
            placeModel.PlaceStatus = Core.Enum.PlaceStatus.DAIRU;
            await _wmsPlaceRep.UpdateAsync(placeModel);

            //任务
            var takmodel = new WmsTask()
            {
                TaskNo = Yitter.IdGenerator.YitIdHelper.NextId().ToString(),
                TaskModel = Core.Enum.TaskModel.ZIDONG,
                TaskType = Core.Enum.TaskType.RUKU,
                TaskLevel = 1,
                Aisle = placeModel.Aisle,
                TaskStatus = Core.Enum.TaskStatus.WEIZHIXING,
                ContainerCode = input.Containercode,
                SourcePlace = input.Entrance,
                ToPlace = input.Placecode,
                AreaName = placeModel.WmsArea.AreaName,
                OrderNo = orderNo,
            };
            await _wmsTaskRep.InsertAsync(takmodel);
        }


        /// <summary>
        /// 托盘解绑
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("UnbindContainer")]
        [UnitOfWork]
        public async Task UnbindContainer([FromQuery] PdaBindEntranceSearchContainer input)
        {
            //托盘库位关系
            var containerPlaceModel = await _wmsContainerPlaceRep.FirstOrDefaultAsync(p => p.ContainerCode == input.Containercode && p.ContainerPlaceStatus == CommonStatus.ENABLE);
            if (containerPlaceModel != null) throw Oops.Oh("存在库位中");

            //任务
            var taskModel = await _wmsTaskRep.FirstOrDefaultAsync(n => n.ContainerCode == input.Containercode && n.TaskStatus != Core.Enum.TaskStatus.WANCHENG);
            if (taskModel != null) throw Oops.Oh("存在任务中");

            var containerModel = await _wmsContainerRep.FirstOrDefaultAsync(n => n.ContainerCode == input.Containercode && n.ContainerStatus == Core.Enum.ContainerStatus.ZUPANG, true);
            if (containerModel == null) throw Oops.Oh("托盘未组盘!");

            var listmodel = await _wmsMaterialContainerRep.Where(p => p.ContainerCode == input.Containercode && p.BindStatus == CommonStatus.ENABLE).ToListAsync();
            foreach (var item in listmodel)
            {
                item.BindStatus = CommonStatus.DISABLE;
                await _wmsMaterialContainerRep.UpdateAsync(item);
            }
            containerModel.ContainerStatus = Core.Enum.ContainerStatus.KOUXIAN;
        }


        /// <summary>
        /// 移库
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("StockTransfer")]
        [UnitOfWork]
        public async Task StockTransfer(PdaAddStockTransferInput input)
        {
            var containerModel = await _wmsContainerRep.FirstOrDefaultAsync(n => n.ContainerCode == input.Containercode && n.ContainerStatus == Core.Enum.ContainerStatus.KUWEI);
            if (containerModel == null) throw Oops.Oh("托盘未在库位中!");

            var oldcontainerPlaceModel = await _wmsContainerPlaceRep.FirstOrDefaultAsync(n => n.ContainerCode == containerModel.ContainerCode && n.ContainerPlaceStatus == CommonStatus.ENABLE);
            if (oldcontainerPlaceModel == null) throw Oops.Oh("库位托盘关系异常!");

            if (oldcontainerPlaceModel.PlaceCode == input.Placecode) throw Oops.Oh("目标库就是原库位");

            var oldplaceModel = await _wmsPlaceRep.FirstOrDefaultAsync(p => p.PlaceCode == oldcontainerPlaceModel.PlaceCode && p.PlaceStatus == Core.Enum.PlaceStatus.CUNHUO);
            if (oldplaceModel == null) throw Oops.Oh("原库位无库位信息!");

            var newplaceModel = await _wmsPlaceRep.DetachedEntities.Where(p => p.PlaceCode == input.Placecode && p.PlaceStatus == Core.Enum.PlaceStatus.KONGXIAN).ProjectToType<WmsPlace>().FirstOrDefaultAsync();
            if (newplaceModel == null) throw Oops.Oh("目标库位未在空闲中!");
            if (newplaceModel.Islock == YesOrNot.Y) throw Oops.Oh("库位被锁定!");

            var materialStockList = await _wmsMaterialStockRep.Where(n => n.PlaceCode == oldplaceModel.PlaceCode && n.ContainerCode == containerModel.ContainerCode).ToListAsync();
            if (materialStockList.Count == 0) throw Oops.Oh("库存中无托盘物料信息!");

            var orderNo = "";
            var materialContainerList = await _wmsMaterialContainerRep.Where(p => p.ContainerCode == input.Containercode && p.BindStatus == CommonStatus.ENABLE).ToListAsync();
            if (materialContainerList.Count > 0) orderNo = materialContainerList.FirstOrDefault().OrderNo;
            else orderNo = "N/A";
            //任务
            var takmodel = new WmsTask()
            {
                TaskNo = Yitter.IdGenerator.YitIdHelper.NextId().ToString(),
                TaskModel = Core.Enum.TaskModel.SHOUDONG,
                TaskType = Core.Enum.TaskType.YIKU,
                TaskLevel = 1,
                Aisle = newplaceModel.Aisle,
                TaskStatus = Core.Enum.TaskStatus.WANCHENG,
                ContainerCode = containerModel.ContainerCode,
                SourcePlace = oldplaceModel.PlaceCode,
                ToPlace = newplaceModel.PlaceCode,
                AreaName = newplaceModel.WmsArea.AreaName,
                OrderNo = orderNo,
            };
            await _wmsTaskRep.InsertAsync(takmodel);


            //更新起点库位信息
            oldcontainerPlaceModel.ContainerPlaceStatus = CommonStatus.DELETED;
            await _wmsContainerPlaceRep.UpdateAsync(oldcontainerPlaceModel);
            oldplaceModel.PlaceStatus = Core.Enum.PlaceStatus.KONGXIAN;
            oldplaceModel.EmptyContainer = YesOrNot.N;
            await _wmsPlaceRep.UpdateAsync(oldplaceModel);


            newplaceModel.PlaceStatus = Core.Enum.PlaceStatus.CUNHUO;
            if (orderNo == "N/A") newplaceModel.EmptyContainer = YesOrNot.Y;
            else newplaceModel.EmptyContainer = YesOrNot.N;
            await _wmsPlaceRep.UpdateAsync(newplaceModel);

            var newcontainerPlaceModel = new WmsContainerPlace()
            {
                PlaceId = newplaceModel.Id,
                PlaceCode = newplaceModel.PlaceCode,
                ContainerId = containerModel.Id,
                ContainerCode = containerModel.ContainerCode,
                ContainerPlaceStatus = CommonStatus.ENABLE,
            };
            await _wmsContainerPlaceRep.InsertAsync(newcontainerPlaceModel);

            foreach (var item in materialStockList)
            {
                item.PlaceCode = newplaceModel.PlaceCode;
                item.AreaId = newplaceModel.WmsArea.Id;
                await _wmsMaterialStockRep.UpdateAsync(item);
            }
        }
    }
}
